<!--
 * @Descripttion: 首页
 * @version: 
 * @Author: Bruce
 * @Date: 2020-11-02 14:24:22
 * @LastEditors: Bruce
 * @LastEditTime: 2020-11-17 17:43:09
-->

<template>
	<div class="container tableFlex">
		<my-table :tableTitle="tableTitle" :tableData="tableData" :loading="loading">
			<template v-slot:action="{ row }">
				<el-button type="text" size="small" @click="goDetails(row)">查看详细</el-button>
				<el-button type="text" size="small" @click="goSignUp(row)" :disabled="filterState(row)">报名</el-button>
			</template>
		</my-table>
		<page :currentPage.sync="currentPage" :total="total" :pageSize="size"></page>
	</div>
</template>

<script>
	import {
		list
	} from "@/api/apiList/exam.js"

	import {
		getExaminee
	} from "@/api/apiList/user.js";
	import { createNamespacedHelpers } from 'vuex'
	const { mapMutations } = createNamespacedHelpers('userInfoModule')
	export default {
		name: "home",
		data() {
			return {
				currentPage: 1, //当前页
				total: 0, //总数
				size: 10, //每页条数
				loading: false,
				tableTitle: [{ //表单标题
						label: '考试时间',
						prop: 'emtm'
					},
					{
						label: '主办单位',
						prop: 'ogze'
					},
					{
						label: '考试名称',
						prop: 'name'
					},
					{
						label: '考试期数',
						prop: 'podl'
					},
					{
						label: '证书名称',
						prop: 'ccnm'
					},
					{
						label: '考试费',
						prop: 'cost'
					},
					{
						label: '报名开始',
						prop: 'star'
					},
					{
						label: '报名截止',
						prop: 'ddle'
					},
					{
						label: '准考证时间',
						prop: 'attm'
					}, {
						label: '报名上限',
						prop: 'tplt'
					}, {
						label: '成绩时间',
						prop: 'gdtm'
					}
				],
				tableData: [] //表格数据
			}
		},
		watch: {
			currentPage: {
				async handler(val) {
					this.loading = true
					let res = await this.API_list(this.size, val)
					let {
						current,
						records
					} = res.data.data
					this.tableData = records
					this.currentPage = current
					this.total = this.tableData.length
					console.log(this.tableData)
					this.loading = false
				},
				immediate: true
			}
		},
		methods: {
			...mapMutations(["UPDATE_USER_INFO"]),
			async API_list(size, current) { //考试列表接口
				let result = {
					page: {
						size,
						current
					},
					param:{
						sta:"" //默认全部 1-可以考试，2-待考试，3-过期
					}
				}
				return await list(result)
			},
			async API_getExaminee() {
				//获取个人信息接口
				return await getExaminee();
			},
			goDetails(row) { //去详情页
				// this.$store.commit("details/UPDATE_ROW", JSON.stringify(row))
				console.log(row)
				this.$router.push({
					path: "/details",
					query:{
						id:row.id
					}
				})
			},
			goSignUp(row) { //去报名表页
				this.$store.commit("signUpForm/UPDATE_ROW", JSON.stringify(row))
				this.$router.push({
					path: "/signupForm",
					query: {
						eid: row.id,
					}
				})
			},
			filterState(obj) { //过滤状态
				let starTime = new Date(obj.star.replace(/-/g, "/")).getTime()//开始日期
				let ddleTime = new Date(obj.ddle.replace(/-/g, "/")).getTime()//结束日期
				let nowTime = new Date().getTime()//现在日期
				let bool = true
				if(starTime < nowTime && nowTime < ddleTime){
					bool = false
				}
				return bool
			}
		},
		async activated() {
			let res = await this.API_getExaminee()
			this.UPDATE_USER_INFO(res.data.data)//更新数据
		}
	}
</script>

<style>

</style>

